﻿Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Drawing
Imports System.Drawing.Drawing2D

Namespace System.Workflow.ComponentModel.Design
    Public Class SequentialWorkflowRootDesigner
        Inherits SequentialActivityDesigner
        ' Methods
        Public Overrides Function CanBeParentedTo(ByVal parentActivityDesigner As CompositeActivityDesigner) As Boolean
            Return False
        End Function

        Protected Overrides Function GetInnerConnections(ByVal edges As DesignerEdges) As ReadOnlyCollection([Of] Point)
            Dim list As New List([Of] Point)(MyBase.GetInnerConnections(edges))
            If (((list.Count > 0) AndAlso (Not Me.Footer Is Nothing)) AndAlso ((edges And DesignerEdges.Bottom) > DesignerEdges.None)) Then
                Dim point As Point = list.Item((list.Count - 1))
                Dim point2 As Point = list.Item((list.Count - 1))
                list.Item((list.Count - 1)) = New Point(point.X, (point2.Y - Me.Footer.Bounds.Height))
            End If
            Return list.AsReadOnly
        End Function

        Friend Sub InternalPerformLayout()
            MyBase.PerformLayout
        End Sub

        Protected Overrides Function OnLayoutSize(ByVal e As ActivityDesignerLayoutEventArgs) As Size
            Dim size As Size = MyBase.OnLayoutSize(e)
            Dim footer As WorkflowFooter = [TryCast](Me.Footer,WorkflowFooter)
            If (Not footer Is Nothing) Then
                size.Height = (size.Height + ((footer.ImageRectangle.Height + (2 * e.AmbientTheme.Margin.Height)) + footer.FooterBarRectangle.Size.Height))
            End If
            If (Not Me.Header Is Nothing) Then
                Me.Header.OnLayout(e)
            End If
            If (Not Me.Footer Is Nothing) Then
                Me.Footer.OnLayout(e)
            End If
            Return size
        End Function

        Protected Overrides Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
            MyBase.OnPaint(e)
            Dim designerTheme As CompositeDesignerTheme = [TryCast](e.DesignerTheme,CompositeDesignerTheme)
            If (Not designerTheme Is Nothing) Then
                If (Not designerTheme.WatermarkImage Is Nothing) Then
                    Dim bounds As Rectangle = MyBase.Bounds
                    bounds.Inflate(-e.AmbientTheme.Margin.Width, -e.AmbientTheme.Margin.Height)
                    ActivityDesignerPaint.GetRectangleFromAlignment(designerTheme.WatermarkAlignment, bounds, designerTheme.WatermarkImage.Size)
                End If
                If (Not Me.Header Is Nothing) Then
                    Me.Header.OnPaint(e)
                End If
                If (Not Me.Footer Is Nothing) Then
                    Me.Footer.OnPaint(e)
                End If
            End If
        End Sub

        Protected Overrides Sub OnSmartTagVisibilityChanged(ByVal visible As Boolean)
            MyBase.OnSmartTagVisibilityChanged(visible)
            If ((Not Me.Header Is Nothing) AndAlso Not Me.Header.TextRectangle.IsEmpty) Then
                MyBase.Invalidate(Me.Header.TextRectangle)
            End If
        End Sub


        ' Properties
        Public Overrides ReadOnly Property CanExpandCollapse As Boolean
            Get
                Return False
            End Get
        End Property

        Protected Overridable ReadOnly Property Footer As SequentialWorkflowHeaderFooter
            Get
                If (Me.footer Is Nothing) Then
                    Me.footer = New WorkflowFooter(Me)
                End If
                Return Me.footer
            End Get
        End Property

        Protected Friend Overrides ReadOnly Property Glyphs As ActivityDesignerGlyphCollection
            Get
                Dim glyphs As New ActivityDesignerGlyphCollection(MyBase.Glyphs)
                If (Not Me.InvokingDesigner Is Nothing) Then
                    glyphs.Add(LockedActivityGlyph.Default)
                End If
                Return glyphs
            End Get
        End Property

        Protected Overridable ReadOnly Property Header As SequentialWorkflowHeaderFooter
            Get
                If (Me.header Is Nothing) Then
                    Me.header = New WorkflowHeader(Me)
                End If
                Return Me.header
            End Get
        End Property

        Public Overrides ReadOnly Property Image As Image
            Get
                Return Me.Header.Image
            End Get
        End Property

        Protected Overrides ReadOnly Property ImageRectangle As Rectangle
            Get
                Return Rectangle.Empty
            End Get
        End Property

        Protected Overrides Property InvokingDesigner As CompositeActivityDesigner
            Get
                Return MyBase.InvokingDesigner
            End Get
            Set(ByVal value As CompositeActivityDesigner)
                MyBase.InvokingDesigner = value
            End Set
        End Property

        Public Overrides ReadOnly Property MinimumSize As Size
            Get
                Dim minimumSize As Size = MyBase.MinimumSize
                minimumSize.Width = Math.Max(minimumSize.Width, SequentialWorkflowRootDesigner.MinSize.Width)
                minimumSize.Height = Math.Max(minimumSize.Width, SequentialWorkflowRootDesigner.MinSize.Height)
                If (MyBase.IsRootDesigner AndAlso (Me.InvokingDesigner Is Nothing)) Then
                    minimumSize.Width = Math.Max(minimumSize.Width, (MyBase.ParentView.ViewPortSize.Width - (2 * DefaultWorkflowLayout.Separator.Width)))
                    minimumSize.Height = Math.Max(minimumSize.Height, (MyBase.ParentView.ViewPortSize.Height - (2 * DefaultWorkflowLayout.Separator.Height)))
                End If
                Return minimumSize
            End Get
        End Property

        Private ReadOnly Property OptimalHeight As Integer
            Get
                Dim designerTheme As CompositeDesignerTheme = [TryCast](MyBase.DesignerTheme,CompositeDesignerTheme)
                If (designerTheme Is Nothing) Then
                    Return 0
                End If
                Dim num As Integer = 0
                If (Me.ContainedDesigners.Count = 0) Then
                    num = (num + designerTheme.ConnectorSize.Height)
                    num = (num + MyBase.HelpTextSize.Height)
                    Return (num + designerTheme.ConnectorSize.Height)
                End If
                Dim activeDesigner As ActivityDesigner = MyBase.ActiveDesigner
                If (activeDesigner Is Me) Then
                    num = (num + designerTheme.ConnectorSize.Height)
                End If
                Dim ambientTheme As AmbientTheme = WorkflowTheme.CurrentTheme.AmbientTheme
                Dim designer2 As ActivityDesigner
                For Each designer2 In Me.ContainedDesigners
                    Dim size As Size = designer2.Size
                    num = (num + size.Height)
                    If (activeDesigner Is Me) Then
                        num = (num + designerTheme.ConnectorSize.Height)
                    Else
                        num = (num + (2 * ambientTheme.SelectionSize.Height))
                    End If
                Next
                Return num
            End Get
        End Property

        Protected Overrides ReadOnly Property ShowSmartTag As Boolean
            Get
                Return ((((Not Me.Header Is Nothing) AndAlso Not String.IsNullOrEmpty(Me.Header.Text)) AndAlso (Me.Views.Count > 1)) OrElse MyBase.ShowSmartTag)
            End Get
        End Property

        Protected Overrides ReadOnly Property SmartTagRectangle As Rectangle
            Get
                Dim empty As Rectangle = Rectangle.Empty
                If (Not Me.Header Is Nothing) Then
                    empty = Me.Header.ImageRectangle
                End If
                Return empty
            End Get
        End Property

        Friend Overrides ReadOnly Property SupportedLayout As WorkflowLayout
            Get
                Return New WorkflowRootLayout(MyBase.Activity.Site)
            End Get
        End Property

        Public Overrides ReadOnly Property [Text] As String
            Get
                Return String.Empty
            End Get
        End Property

        Protected Overrides ReadOnly Property TitleHeight As Integer
            Get
                Dim titleHeight As Integer = MyBase.TitleHeight
                If (Not Me.Header Is Nothing) Then
                    titleHeight = (titleHeight + Me.Header.Bounds.Height)
                End If
                Return titleHeight
            End Get
        End Property


        ' Fields
        Private footer As WorkflowFooter
        Private Shared ReadOnly FooterImage As Image = DR.GetImage("EndWorkflow")
        Private header As WorkflowHeader
        Private Const HeaderFooterSizeIncr As Integer = 8
        Private Shared ReadOnly HeaderImage As Image = DR.GetImage("StartWorkflow")
        Private Shared ReadOnly MinSize As Size = New Size(240, 240)
        Private Shared ReadOnly PageStripItemSize As Size = New Size(&H18, 20)

        ' Nested Types
        Private NotInheritable Class WorkflowFooter
            Inherits SequentialWorkflowHeaderFooter
            ' Methods
            Public Sub New(ByVal parent As SequentialWorkflowRootDesigner)
                MyBase.New(parent, False)
                Me.Image = SequentialWorkflowRootDesigner.FooterImage
            End Sub

            Public Overrides Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
                If (e Is Nothing) Then
                    Throw New ArgumentNullException("e")
                End If
                Dim footerBarRectangle As Rectangle = Me.FooterBarRectangle
                If Not Me.FooterBarRectangle.IsEmpty Then
                    Dim empty As Color = Color.Empty
                    Dim color2 As Color = Color.FromArgb(50, e.DesignerTheme.BorderColor)
                    [Using] brush As Brush = New LinearGradientBrush(footerBarRectangle, color2, empty, LinearGradientMode.Vertical)
                        e.Graphics.FillRectangle(brush, footerBarRectangle)
                        e.Graphics.DrawLine(e.DesignerTheme.BorderPen, footerBarRectangle.Left, footerBarRectangle.Top, footerBarRectangle.Right, footerBarRectangle.Top)
                    End [Using]
                End If
                MyBase.OnPaint(e)
            End Sub


            ' Properties
            Public Overrides ReadOnly Property Bounds As Rectangle
                Get
                    Dim bounds As Rectangle = MyBase.Bounds
                    Dim associatedDesigner As SequentialWorkflowRootDesigner = MyBase.AssociatedDesigner
                    bounds.Height = Math.Max(bounds.Height, ((associatedDesigner.Size.Height - associatedDesigner.TitleHeight) - associatedDesigner.OptimalHeight))
                    bounds.Y = ((associatedDesigner.Location.Y + associatedDesigner.TitleHeight) + associatedDesigner.OptimalHeight)
                    Dim height As Integer = Me.ImageRectangle.Height
                    height = (height + If((height > 0), (2 * WorkflowTheme.CurrentTheme.AmbientTheme.Margin.Height), 0))
                    height = (height + Me.MinFooterBarHeight)
                    bounds.Height = Math.Max(height, bounds.Height)
                    Return bounds
                End Get
            End Property

            Friend ReadOnly Property FooterBarRectangle As Rectangle
                Get
                    Return Rectangle.Empty
                End Get
            End Property

            Public Overrides ReadOnly Property ImageRectangle As Rectangle
                Get
                    Dim imageRectangle As Rectangle = MyBase.ImageRectangle
                    If (Not Me.Image Is Nothing) Then
                        Dim associatedDesigner As SequentialWorkflowRootDesigner = MyBase.AssociatedDesigner
                        imageRectangle.X = (imageRectangle.X - 4)
                        imageRectangle.Width = (imageRectangle.Width + 8)
                        imageRectangle.Height = (imageRectangle.Height + 8)
                        imageRectangle.Y = ((associatedDesigner.Location.Y + associatedDesigner.TitleHeight) + associatedDesigner.OptimalHeight)
                        imageRectangle.Y = (imageRectangle.Y + WorkflowTheme.CurrentTheme.AmbientTheme.Margin.Height)
                    End If
                    Return imageRectangle
                End Get
            End Property

            Private ReadOnly Property MinFooterBarHeight As Integer
                Get
                    Return 0
                End Get
            End Property

        End Class

        Private NotInheritable Class WorkflowHeader
            Inherits SequentialWorkflowHeaderFooter
            ' Methods
            Public Sub New(ByVal parent As SequentialWorkflowRootDesigner)
                MyBase.New(parent, True)
                Me.Image = SequentialWorkflowRootDesigner.HeaderImage
            End Sub

            Public Overrides Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
                If (e Is Nothing) Then
                    Throw New ArgumentNullException("e")
                End If
                Dim headerBarRectangle As Rectangle = Me.HeaderBarRectangle
                Dim empty As Color = Color.Empty
                Dim color2 As Color = Color.FromArgb(50, e.DesignerTheme.BorderColor)
                [Using] brush As Brush = New LinearGradientBrush(headerBarRectangle, empty, color2, LinearGradientMode.Vertical)
                    e.Graphics.FillRectangle(brush, headerBarRectangle)
                    e.Graphics.DrawLine(e.DesignerTheme.BorderPen, headerBarRectangle.Left, headerBarRectangle.Bottom, headerBarRectangle.Right, headerBarRectangle.Bottom)
                End [Using]
                MyBase.OnPaint(e)
            End Sub


            ' Properties
            Public Overrides ReadOnly Property Bounds As Rectangle
                Get
                    Dim bounds As Rectangle = MyBase.Bounds
                    Dim textRectangle As Rectangle = MyBase.TextRectangle
                    If (Me.MinHeaderBarHeight > textRectangle.Height) Then
                        bounds.Height = (bounds.Height + (Me.MinHeaderBarHeight - textRectangle.Height))
                    End If
                    Return bounds
                End Get
            End Property

            Private ReadOnly Property HeaderBarRectangle As Rectangle
                Get
                    Return New Rectangle { _
                        .Location = MyBase.AssociatedDesigner.Location, _
                        .Width = MyBase.AssociatedDesigner.Size.Width, _
                        .Height = Math.Max(((2 * WorkflowTheme.CurrentTheme.AmbientTheme.Margin.Height) + Me.textSize.Height), Me.MinHeaderBarHeight) _
                    }
                End Get
            End Property

            Public Overrides ReadOnly Property ImageRectangle As Rectangle
                Get
                    Dim imageRectangle As Rectangle = MyBase.ImageRectangle
                    If (Not Me.Image Is Nothing) Then
                        Dim designerTheme As ActivityDesignerTheme = MyBase.AssociatedDesigner.DesignerTheme
                        imageRectangle.X = (imageRectangle.X - 4)
                        imageRectangle.Y = (Me.HeaderBarRectangle.Bottom + WorkflowTheme.CurrentTheme.AmbientTheme.Margin.Height)
                        imageRectangle.Width = (imageRectangle.Width + 8)
                        imageRectangle.Height = (imageRectangle.Height + 8)
                    End If
                    Return imageRectangle
                End Get
            End Property

            Private ReadOnly Property MinHeaderBarHeight As Integer
                Get
                    Return (2 * WorkflowTheme.CurrentTheme.AmbientTheme.Margin.Height)
                End Get
            End Property

            Public Overrides ReadOnly Property TextRectangle As Rectangle
                Get
                    Dim textRectangle As Rectangle = MyBase.TextRectangle
                    If (Me.MinHeaderBarHeight > textRectangle.Height) Then
                        textRectangle.Y = (textRectangle.Y + ((Me.MinHeaderBarHeight - textRectangle.Height) / 2))
                    End If
                    Return textRectangle
                End Get
            End Property

        End Class
    End Class
End Namespace

